/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-03-21     huger       the first version
 */
#ifndef APPLICATIONS_APP_BLDC_MOTOR_H_
#define APPLICATIONS_APP_BLDC_MOTOR_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


#include "bsp\bsp.h"

#define PWM_TIM_PULSE_TPWM            PWM_PERIOD//(CKTIM/(PWM_FREQ)/2)
#define Udc 24.0f
#define PI                            3.14159265358979f
#define ADC_REF_V                     (float)(3.3f)
#define SAMPLE_RES                    (double)(0.05)
#define AMP_GAIN                      (double)(5.0)     //
#define SAMPLE_CURR_CON_FACTOR        (double)(ADC_REF_V/4096.0/AMP_GAIN/SAMPLE_RES)


#define foc_cyclic_time               20
#define PID_SAMPLE_TIME               ((uint16_t)(PWM_PERIOD*2*foc_cyclic_time)/72)
#define SPEED_SAMPLE_FREQ             (uint16_t)(1000000/PID_SAMPLE_TIME)


#define Start_V 2.5f
#define cpr (float)(2.0f*PI)
#define Max_Vbus 24.0F

#define POLE_PAIR_NUM   2   //磁极数 4

typedef enum
{
IDLE, INIT, START, RUN, STOP, BRAKE, WAIT, FAULT
} SystStatus_t;

typedef enum
{
pos=0, spd, curr
}RunMode;

extern float Speed_Pid_Out;
extern float ID_REF;
extern float IQ_REF;
extern float Speed_Ref;
extern float Posi_Ref;
extern uint8_t Motor_Run_Mode;

extern float magnetic_current_pos;

void foc_algorithm_step(void);
void Motor_control_thread_init(void);
void Start_Up(void);
float_t ENC_Get_Electrical_Angle(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* APPLICATIONS_APP_BLDC_MOTOR_H_ */
